﻿/*
    File name:          ConcreteDLLAdaptor
    File type:          Adaptor Class
    Author:             Sune Krog-Meyer 09888
    Creation date:      03-04-2011
    Description:        Contains all funktions for the robot - Check the Hardwaredll for specific information of the funktions.
*/
using System;
using i4pro_gruppe_d.ControllerCommunication;
using i4pro_gruppe_d.DLL;
using i4pro_gruppe_d.Types;

namespace i4pro_gruppe_d.RobotCommunication
{
	public class ConcreteDLLAdaptor : IControllerCommunication
    {
        private bool MotionEventCheck { get; set; }
        private bool conveyerMove { get; set; }

        SerialSTK stk = new SerialSTK();

        public ConcreteDLLAdaptor()
        {            
        }

        #region Init and Control Funktions

        public bool home(char axis)
        {
            return DLLHandle.Homing((byte)axis, homeEventHandle);
        }

        public void homeEventHandle(Byte homeNotif)
        {            
        }


        public bool control(char axis, bool status)
        {
            return DLLHandle.ControlOn((byte)axis, status);
        }

        public bool init(ushort mode)
        {
            return DLLHandle.Init(mode, 0, dataEventHandle, errorEventHandle);
        }

        public void dataEventHandle(IntPtr dataConfig)
        {
        }

        public void errorEventHandle(IntPtr errorInfo)
        {
        }


        #endregion

        #region Gripper Funktions

        public bool openGripper()
        {
            return DLLHandle.OpenGripper();
        }

        public bool closeGripper()
        {
            return DLLHandle.CloseGripper();
        }

        public bool getGripperWidth(ref short width)
        {
            short percent = 0;
            return DLLHandle.GetJaw(ref percent, ref width);
        }

        #endregion

        #region Conveyer Funktions

        public bool startConveyer(int speed)
        {
            WatchMotions(false, false);
            MotionEventCheck = false;
            activateLightsensor();
            enterManualMode();
            conveyerMove = true;
            return DLLHandle.MoveManual((byte)('8' - 49), speed);
        }

        public bool stopConveyer()
        {
            bool temp = DLLHandle.Stop((byte)'&');            
            return temp;
        }

        public bool enterManualMode(short moveType = 0)
        {
            bool temp = DLLHandle.EnterManual(moveType);
            Console.WriteLine("Enter Manual move returned: " + temp);
            return temp;
        }

        public bool closeManualMode()
        {
            return DLLHandle.CloseManual();
        }

        #endregion

        #region Vector Funktions

        public bool defineVector(char group, string vectorName, short size)
        {
            return DLLHandle.DefineVector((byte)group, vectorName, size);
        }

        public bool teach(string vectorName, Vector vector)
        {
            int[] coordArray = { vector.x, vector.y, vector.z, vector.pitch, vector.roll };
            return DLLHandle.Teach(vectorName, (short)vector.pointNumber, coordArray, (short)coordArray.Length, vector.type);
        }

        public bool getCurrentPos(ref Vector pos)
        {
            return DLLHandle.getPos( new Vector(), new Vector(), pos);
        }

        #endregion

        #region Watch Inputs
        
        public bool activateLightsensor()
        {
            bool temp = DLLHandle.StartWatchDigital(digitalEventHandle);
            System.Threading.Thread.Sleep(500);
            Console.WriteLine("Activate Lightsensor returned: " + temp);
            return temp;
        }

        public void digitalEventHandle(Int32 input)
        {
            if (conveyerMove)
            {
                stopConveyer();
                Console.WriteLine("Stopping Conveyer");
                deactivateLightsensor();
                closeManualMode();
                conveyerMove = false;
                MotionEventCheck = true;
            }
        }

        public bool deactivateLightsensor()
        {
            Console.WriteLine("Deactivating Lightsensor");
            return DLLHandle.StopWatchDigit();
        }

        public double measureWeight()
        {
            stk.Open();
            
            double tempWeight = stk.ReadADC();
 
            stk.Close();

            return tempWeight;
        }

        #endregion

        #region Movement Funktions

        public bool moveTo(string vectorName1, short pointNumber1, string vectorName2, short pointNumber2)
        {
            return DLLHandle.MoveLinear(vectorName1, pointNumber1, null, pointNumber2);
        }

        public void WatchMotions(bool endMotions, bool startMotions)
        {
            if (endMotions && startMotions)
                DLLHandle.WatchMotion(endMotionEventHandler, startMotionEventHander);
            else if (endMotions && !startMotions)
                DLLHandle.WatchMotion(endMotionEventHandler, null);
            else if (!endMotions && startMotions)
                DLLHandle.WatchMotion(null, startMotionEventHander);
            else
                DLLHandle.WatchMotion(null, null);
        }

        public void endMotionEventHandler(IntPtr end)
        {
            MotionEventCheck = true;
        }

        public void startMotionEventHander(IntPtr start)
        {
            MotionEventCheck = false;
        }

        public bool GetMotionStatus()
        {
            return MotionEventCheck;            
        }

        #endregion

        #region Utility Funktions

        public bool stopRobot(char axis)
        {
            return DLLHandle.Stop((byte)axis);
        }

        public bool isRobotOnline()
        {
            return DLLHandle.IsOnline();
        }

        public bool isEmergency()
        {
            return DLLHandle.IsEmergency();
        }

        public void closeConnection()
        {
            DLLHandle.closeConnection();
        }

        #endregion
    }
}
